<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/2/20
 * Time: 11:31
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Tens extends Base
{
    /**
     * 轮播图首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            extract(input());
            $where = [];
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('ten')->where($where)->count();// 获取总条数
            $lists = Db::name('ten')
                ->page($Nowpage,$limits)
                ->order('ten_status asc,ten_sort desc,ten_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    

    /**
     * 编辑轮播图
     * @return mixed|\think\response\Json
     */
    public function bannerEdit(){
        if(Request::instance()->isPost()){
            extract(input());
            $data = [
                'ten_name' => $ten_name,
                'ten_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_ten')->where('ten_id', $ten_id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $ten_id = input('ten_id');
        // banner
        $banner = Db::table('up_ten')
            ->where('ten_id', $ten_id)
            ->find();
        $this->assign([
            'banner' => $banner
        ]);
        return $this->fetch('tens/banneredit');
    }
    
    /**
     * 排序编辑
     * @return mixed|\think\response\Json
     */
    public function updateSort(){
        $id = input('id');
        $param = [
            'ten_sort'=>input('value'),
        ];
        $res = Db::name('ten')->where('ten_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
    
    /**
     * 上架
     * @return \think\response\Json
     */
    public function bannerOn(){
        $banner_id = input('id');
        $res = Db::table('up_ten')->where('ten_id', $banner_id)->setField('ten_status', 1);
        if($res){
            return json(['code' => 200, 'msg' => '上架成功']);
        }else{
            return json(['code' => 100, 'msg' => '上架失败']);
        }
    }
    
    /**
     * 下架
     * @return \think\response\Json
     */
    public function bannerDown(){
        $banner_id = input('id');
        $res = Db::table('up_ten')->where('ten_id', $banner_id)->setField('ten_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '下架成功']);
        }else{
            return json(['code' => 100, 'msg' => '下架失败']);
        }
    }

}